import { Prop, Provide, Emit, Model } from 'vue-property-decorator';
import { Subject, Subscription } from 'rxjs';
import { UIActionTool,Util,ViewTool } from '@/utils';
import { Watch, GridControlBase } from '@/studio-core';
import EMAssetService from '@/service/emasset/emasset-service';
import Main4Service from './main4-grid-service';
import EMAssetUIService from '@/uiservice/emasset/emasset-ui-service';
import { FormItemModel } from '@/model/form-detail';


/**
 * grid部件基类
 *
 * @export
 * @class GridControlBase
 * @extends {Main4GridBase}
 */
export class Main4GridBase extends GridControlBase {

    /**
     * 获取部件类型
     *
     * @protected
     * @type {string}
     * @memberof Main4GridBase
     */
    protected controlType: string = 'GRID';

    /**
     * 建构部件服务对象
     *
     * @type {Main4Service}
     * @memberof Main4GridBase
     */
    public service: Main4Service = new Main4Service({ $store: this.$store });

    /**
     * 实体服务对象
     *
     * @type {EMAssetService}
     * @memberof Main4GridBase
     */
    public appEntityService: EMAssetService = new EMAssetService({ $store: this.$store });

    /**
     * 应用实体名称
     *
     * @protected
     * @type {string}
     * @memberof Main4GridBase
     */
    protected appDeName: string = 'emasset';

    /**
     * 应用实体中文名称
     *
     * @protected
     * @type {string}
     * @memberof Main4GridBase
     */
    protected appDeLogicName: string = '资产';

    /**
     * 界面UI服务对象
     *
     * @type {EMAssetUIService}
     * @memberof Main4Base
     */  
    public appUIService:EMAssetUIService = new EMAssetUIService(this.$store);


    /**
     * 界面行为模型
     *
     * @type {*}
     * @memberof Main4Base
     */  
    public ActionModel: any = {
    };

    /**
     * 主信息表格列
     *
     * @type {string}
     * @memberof Main4Base
     */  
    public majorInfoColName:string = "emassetname";


    /**
     * 本地缓存标识
     *
     * @protected
     * @type {string}
     * @memberof Main4Base
     */
    protected localStorageTag: string = 'emasset_main4_grid';

    /**
     * 所有列成员
     *
     * @type {any[]}
     * @memberof Main4GridBase
     */
    public allColumns: any[] = [
        {
            name: 'assetcode',
            label: '资产代码',
            langtag: 'entities.emasset.main4_grid.columns.assetcode',
            show: true,
            unit: 'PX',
            isEnableRowEdit: false,
            enableCond: 3 ,
        },
        {
            name: 'num',
            label: '第几号',
            langtag: 'entities.emasset.main4_grid.columns.num',
            show: true,
            unit: 'PX',
            isEnableRowEdit: false,
            enableCond: 3 ,
        },
        {
            name: 'emassetname',
            label: '资产名称',
            langtag: 'entities.emasset.main4_grid.columns.emassetname',
            show: true,
            unit: 'PX',
            isEnableRowEdit: false,
            enableCond: 3 ,
        },
        {
            name: 'assettype',
            label: '资产类别',
            langtag: 'entities.emasset.main4_grid.columns.assettype',
            show: true,
            unit: 'PX',
            isEnableRowEdit: false,
            enableCond: 3 ,
        },
        {
            name: 'assetlct',
            label: '存放地点',
            langtag: 'entities.emasset.main4_grid.columns.assetlct',
            show: true,
            unit: 'PX',
            isEnableRowEdit: false,
            enableCond: 3 ,
        },
        {
            name: 'eqmodelcode',
            label: '规格型号',
            langtag: 'entities.emasset.main4_grid.columns.eqmodelcode',
            show: true,
            unit: 'PX',
            isEnableRowEdit: false,
            enableCond: 3 ,
        },
        {
            name: 'assetclassname',
            label: '资产科目',
            langtag: 'entities.emasset.main4_grid.columns.assetclassname',
            show: true,
            unit: 'PX',
            isEnableRowEdit: false,
            enableCond: 3 ,
        },
        {
            name: 'eqlife',
            label: '使用期限',
            langtag: 'entities.emasset.main4_grid.columns.eqlife',
            show: true,
            unit: 'PX',
            isEnableRowEdit: false,
            enableCond: 3 ,
        },
        {
            name: 'purchdate',
            label: '采购日期',
            langtag: 'entities.emasset.main4_grid.columns.purchdate',
            show: true,
            unit: 'PX',
            isEnableRowEdit: false,
            enableCond: 3 ,
        },
        {
            name: 'eqstartdate',
            label: '投运日期',
            langtag: 'entities.emasset.main4_grid.columns.eqstartdate',
            show: true,
            unit: 'PX',
            isEnableRowEdit: false,
            enableCond: 3 ,
        },
        {
            name: 'disdate',
            label: '报废日期',
            langtag: 'entities.emasset.main4_grid.columns.disdate',
            show: true,
            unit: 'PX',
            isEnableRowEdit: false,
            enableCond: 3 ,
        },
        {
            name: 'disdesc',
            label: '报废原因',
            langtag: 'entities.emasset.main4_grid.columns.disdesc',
            show: true,
            unit: 'PX',
            isEnableRowEdit: false,
            enableCond: 3 ,
        },
        {
            name: 'discost',
            label: '残值',
            langtag: 'entities.emasset.main4_grid.columns.discost',
            show: true,
            unit: 'PX',
            isEnableRowEdit: false,
            enableCond: 3 ,
        },
        {
            name: 'deptname',
            label: '使用部门',
            langtag: 'entities.emasset.main4_grid.columns.deptname',
            show: true,
            unit: 'PX',
            isEnableRowEdit: false,
            enableCond: 3 ,
        },
        {
            name: 'empname',
            label: '使用人',
            langtag: 'entities.emasset.main4_grid.columns.empname',
            show: true,
            unit: 'PX',
            isEnableRowEdit: false,
            enableCond: 3 ,
        },
        {
            name: 'unitname',
            label: '单位',
            langtag: 'entities.emasset.main4_grid.columns.unitname',
            show: true,
            unit: 'PX',
            isEnableRowEdit: false,
            enableCond: 3 ,
        },
        {
            name: 'originalcost',
            label: '资产原值',
            langtag: 'entities.emasset.main4_grid.columns.originalcost',
            show: true,
            unit: 'PX',
            isEnableRowEdit: false,
            enableCond: 3 ,
        },
        {
            name: 'ytzj',
            label: '已提折旧',
            langtag: 'entities.emasset.main4_grid.columns.ytzj',
            show: true,
            unit: 'PX',
            isEnableRowEdit: false,
            enableCond: 3 ,
        },
        {
            name: 'replacerate',
            label: '残值率(%)',
            langtag: 'entities.emasset.main4_grid.columns.replacerate',
            show: true,
            unit: 'PX',
            isEnableRowEdit: false,
            enableCond: 3 ,
        },
        {
            name: 'mgrdeptname',
            label: '管理部门',
            langtag: 'entities.emasset.main4_grid.columns.mgrdeptname',
            show: true,
            unit: 'PX',
            isEnableRowEdit: false,
            enableCond: 3 ,
        },
        {
            name: 'rempname',
            label: '经办人',
            langtag: 'entities.emasset.main4_grid.columns.rempname',
            show: true,
            unit: 'PX',
            isEnableRowEdit: false,
            enableCond: 3 ,
        },
        {
            name: 'eqserialcode',
            label: '车架号',
            langtag: 'entities.emasset.main4_grid.columns.eqserialcode',
            show: true,
            unit: 'PX',
            isEnableRowEdit: false,
            enableCond: 3 ,
        },
        {
            name: 'blsystemdesc',
            label: '发动机号',
            langtag: 'entities.emasset.main4_grid.columns.blsystemdesc',
            show: true,
            unit: 'PX',
            isEnableRowEdit: false,
            enableCond: 3 ,
        },
        {
            name: 'keyattparam',
            label: '设备代码',
            langtag: 'entities.emasset.main4_grid.columns.keyattparam',
            show: true,
            unit: 'PX',
            isEnableRowEdit: false,
            enableCond: 3 ,
        },
        {
            name: 'now',
            label: '资产余值',
            langtag: 'entities.emasset.main4_grid.columns.now',
            show: true,
            unit: 'PX',
            isEnableRowEdit: false,
            enableCond: 3 ,
        },
    ]

    /**
     * 获取表格行模型
     *
     * @type {*}
     * @memberof Main4GridBase
     */
    public getGridRowModel(){
        return {
          srfkey: new FormItemModel(),
        }
    }

    /**
     * 属性值规则
     *
     * @type {*}
     * @memberof Main4GridBase
     */
    public rules: any = {
        srfkey: [
            { required: false, validator: (rule:any, value:any, callback:any) => { return (rule.required && (value === null || value === undefined || value === "")) ? false : true;}, message: '资产标识 值不能为空', trigger: 'change' },
            { required: false, validator: (rule:any, value:any, callback:any) => { return (rule.required && (value === null || value === undefined || value === "")) ? false : true;}, message: '资产标识 值不能为空', trigger: 'blur' },
        ],
    }

    /**
     * 属性值规则
     *
     * @type {*}
     * @memberof Main4Base
     */
    public deRules:any = {
    };

    /**
     * 获取对应列class
     *
     * @type {*}
     * @memberof Main4Base
     */
    public hasRowEdit: any = {
        'assetcode':false,
        'num':false,
        'emassetname':false,
        'assettype':false,
        'assetlct':false,
        'eqmodelcode':false,
        'assetclassname':false,
        'eqlife':false,
        'purchdate':false,
        'eqstartdate':false,
        'disdate':false,
        'disdesc':false,
        'discost':false,
        'deptname':false,
        'empname':false,
        'unitname':false,
        'originalcost':false,
        'ytzj':false,
        'replacerate':false,
        'mgrdeptname':false,
        'rempname':false,
        'eqserialcode':false,
        'blsystemdesc':false,
        'keyattparam':false,
        'now':false,
    };

    /**
     * 获取对应列class
     *
     * @param {*} $args row 行数据，column 列数据，rowIndex 行索引，列索引
     * @returns {void}
     * @memberof Main4Base
     */
    public getCellClassName(args: {row: any, column: any, rowIndex: number, columnIndex: number}): any {
        return ( this.hasRowEdit[args.column.property] && this.actualIsOpenEdit ) ? "edit-cell" : "info-cell";
    }


    /**
     * 导出数据格式化
     *
     * @param {*} filterVal
     * @param {*} jsonData
     * @param {any[]} [codelistColumns=[]]
     * @returns {Promise<any>}
     * @memberof Main4GridBase
     */
    public async formatExcelData(filterVal: any, jsonData: any, codelistColumns?: any[]): Promise<any> {
        return super.formatExcelData(filterVal, jsonData, [
            {
                name: 'assettype',
                srfkey: 'EMASSETTYPE',
                codelistType : 'STATIC',
                renderMode: 'other',
                textSeparator: '、',
                valueSeparator: ',',
            },
        ]);
    }


    /**
     * 更新默认值
     * @param {*}  row 行数据
     * @memberof Main4Base
     */
    public updateDefault(row: any){                    
    }

    /**
     * 计算数据对象类型的默认值
     * @param {string}  action 行为
     * @param {string}  param 默认值参数
     * @param {*}  data 当前行数据
     * @memberof Main4Base
     */
    public computeDefaultValueWithParam(action:string,param:string,data:any){
        if(Object.is(action,"UPDATE")){
            const nativeData:any = this.service.getCopynativeData();
            if(nativeData && (nativeData instanceof Array) && nativeData.length >0){
                let targetData:any = nativeData.find((item:any) =>{
                    return item.emassetid === data.srfkey;
                })
                if(targetData){
                    return targetData[param]?targetData[param]:null;
                }else{
                    return null;
                }
            }else{
                return null;
            }
        }else{
           return this.service.getRemoteCopyData()[param]?this.service.getRemoteCopyData()[param]:null;
        }
    }


}